package atm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM {
    public static void main(String[] args) {
        ArrayList<user> list = new ArrayList<>();
        while (true) {
            System.out.println("\t" + "欢迎使用于氏银行登录系统" + "\t");
            System.out.println("1.登录账户");
            System.out.println("2.注册账户");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择您要办理的业务");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    welcome(list);
                    break;
                case 2:
                    register(list);
                    break;
                default:
                    System.out.println("您输入的指令有误！！！");
            }
        }
    }

    /**
     * 注册方法
     * @param list
     */
    private static void register(ArrayList<user> list) {
        while (true) {
            user u = new user();
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的姓名");
            String name = sc.next();
            u.setName(name);
            System.out.println("请输入您要设置的卡号密码");
            String passWorld1 = sc.next();
            System.out.println("请确认您的卡号密码");
            String passWorld2 = sc.next();
            if(passWorld1.equals(passWorld2)) {
                u.setPassWorld(passWorld1);
                System.out.println("密码设置成功");
                System.out.println("设置您当日每次取钱额度");
                int limit =sc.nextInt();
                u.setLimit(limit);
                String ue = create(list);
                u.setCardNumb(ue);
                list.add(u);
                System.out.println("设置成功");
                System.out.println("开户名："+u.getName()+"\t"+"卡号："+u.getCardNumb()+"\t"+"余额"+u.getBalance());
                break;
            } else {
                System.out.println("您俩次密码设置不同，请重新设置");
            }
        }
    }

    /**
     * 创建卡号的方法
     * @param list
     * @return
     */
    public static String create(ArrayList<user> list) {
        while (true) {
            String card = "";
            Random r = new Random();
            for(int i = 0;i<8;i++) {
                card += r.nextInt(10);
            }
            user uu = getByCard(card, list);
            if(uu==null) {
                return card;
            }
        }
    }

    /**
     * 防止有重复的卡号
     * @param card
     * @param list
     * @return
     */
    public static user getByCard(String card , ArrayList<user> list){
        for (int i = 0; i < list.size(); i++) {
          user acc = list.get(i);
            if(acc.getCardNumb().equals(card)){
                return acc;
            }
        }
        return null;
    }
    public static void welcome(ArrayList<user> list) {
        while (true) {
            System.out.println("\t"+"欢迎来到登录界面"+"\t");
            System.out.println("请输入您的卡号");
            Scanner sc = new Scanner(System.in);
            String userPass = sc.next();
            for (int i = 0; i < list.size(); i++) {
                user u1 = list.get(i);
                if(u1.getCardNumb().equals(userPass)) {
                    while (true) {
                        System.out.println("欢迎来到操作界面");
                        System.out.println("请选择您要执行的操作");
                        System.out.println("1.存款");
                        System.out.println("2.取款");
                        System.out.println("3.转账");
                        System.out.println("4.查看余额");
                        System.out.println("5.更改密码");
                        System.out.println("6.退出");
                        int c = sc.nextInt();
                        switch(c) {
                            case 1:
                                cang(u1,sc);
                                break;
                            case 2:
                                getMoney(u1,sc);
                                break;
                            case 3:
                                giveMoney(u1,sc);
                                break;
                            case 4:
                                showMoney(u1);
                                break;
                            case 5:
                                change(u1,sc);
                                break;
                            case 6:
                                System.out.println("欢迎下次继续使用于氏银行");
                                return;
                            default:
                                System.out.println("您操作错误，请重新选择");
                        }
                    }
                } else {
                    System.out.println("您输入的卡号不存在,请先注册该账号");
                }
            }
        }
    }
    public static void cang(user u,Scanner sc) {
        System.out.println("\t"+"欢迎使用存储业务"+"\t");
        System.out.println("请输入您要存的金额");
        int i = sc.nextInt();
        u.setBalance(u.getBalance()+i);
        System.out.println("当前您的账户余额为"+u.getBalance());
    }
    public static void getMoney(user u,Scanner sc) {
        System.out.println("\t"+"欢迎使用取款业务"+"\t");
        System.out.println("请输入您要取的金额");
        int q = sc.nextInt();
        if(q<=u.getBalance()) {
            u.setBalance(u.getBalance()-q);
            System.out.println("取款成功啦，您当前账户还有"+u.getBalance());
        } else {
            System.out.println("您取的太多啦，余额不足");
        }
    }
    public static void giveMoney(user u,Scanner sc) {
        System.out.println("\t"+"欢迎使用转账业务"+"\t");
        System.out.println("请输入要转账的卡号");
        String cn = sc.next();
        if(u.getCardNumb().equals(cn)) {
            System.out.println("请输入您要转账的金额数量");
            int m = sc.nextInt();
            if(m<=u.getBalance()) {
                u.setBalance(u.getBalance()-m);
                System.out.println("转账成功啦，您当前余额为"+u.getBalance());
            } else {
                System.out.println("您自己都没钱啦，就别转给别人啦");
            }
        } else {
            System.out.println("查无此卡号，请重新输入");
        }
    }
    public static void showMoney(user u) {
        System.out.println("\t"+"欢迎使用余额查询业务"+"\t");
        System.out.println("您的账户余额为"+u.getBalance());
    }
    public static void change(user u,Scanner sc) {
        while (true) {
            System.out.println("\t"+"欢迎使用更改密码功能"+"\t");
            System.out.println("请注意周边环境是否安全");
            System.out.println("请输入您的原来密码，并确认是否要更改密码");
            String c = sc.next();
            if(u.getPassWorld().equals(c)) {
                System.out.println("请您输入您要设置的密码");
                String h =sc.next();
                System.out.println("请您再次输入您要设置的密码");
                String e = sc.next();
                if(h.equals(e)) {
                    u.setPassWorld(h);
                    System.out.println("密码更改成功");
                    break;
                } else {
                    System.out.println("俩次输入密码不一致，请重新更改密码");
                }
            }
        }
    }
}
